package daoimp;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.JDBCTemplate;
import dao.NewsDao;
import entity.News;

public class NewsDaoImp implements NewsDao
{
    JDBCTemplate jt;
    public NewsDaoImp(){
        jt=new JDBCTemplate(); 
    }
    
    public int add(News a) throws SQLException
    {
        
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
        return jt.executeupdate("insert into news(nid,ntitle,nauthor,ntext,ncreatedate) values(?,?,?,?,?)",a.getNid(),a.getNtitle(),a.getNauthor(),a.getNtext(),sd.format(new Date()));    }
    
    public int delete(int aid) throws SQLException
    {
        return jt.executeupdate("delete from news where nid=?",aid);
    }
    
    public int update(News a) throws SQLException
    {
        return jt.executeupdate("update news set ntitle=?,nauthor=?,ntext=?where nid=?",a.getNtitle(),
        		a.getNauthor(),a.getNtext(),a.getNid());
    }
    
    public List query() throws SQLException
    {
        return jt.executequery("select * from news",News.class, null);
    }

    public News selectId(int id)
        throws SQLException
    {
        List<News> list = new ArrayList();
        list = jt.executequery("select*from news where nid=?", News.class, id);
        return list.size() > 0 ? list.get(0) : null;
    }
}
